<#include "include/head.ftl">
<#macro mapperEl value>${r"#{"}${value}}</#macro>
<#macro jspEl value>${r"${"}${value}}</#macro>
<#macro conditionMapperEl value>${r"#{condition."}${value}}</#macro>
<#assign idJavaType="java.lang.Long">
<#list table.columnList as column>
    <#if column.columnName?lower_case=='id'>
        <#assign idJavaType = column.columnClassName>
    </#if>
</#list>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${NamespaceDomain}.${Po}Entity">
	<resultMap type="${NamespaceDomain}.${Po}Entity" id="BaseResultMap">
	  <#list table.columnList as column>
      <result column="${column.fieldName}" property="${column.columnName}"/>
	  </#list>
	</resultMap>
	
	<!-- 公共条件 -->
	<sql id="Conditions_Where_Clause">
	   <trim prefix="WHERE" prefixOverrides="AND | OR">
	 	  <foreach collection="oredCriteria" item="o" separator="or">
		   <if test="o.valid">
		    (
		     <foreach collection="o.criteriaWithoutValue" item="oc" separator="and"> <@jspEl 'oc'/> </foreach>
		     
		     <if test="o.criteriaWithoutValue.size!=0 and o.criteriaWithSingleValue.size!=0">
		       and
		     </if>
		     <foreach collection="o.criteriaWithSingleValue" item="os" separator="and"> <@jspEl 'os.condition'/> <@mapperEl 'os.value'/> </foreach>
		     
		     <if test="(o.criteriaWithoutValue.size!=0 or o.criteriaWithSingleValue.size!=0) and o.criteriaWithListValue.size!=0">
		       and
		     </if>
		     <foreach collection="o.criteriaWithListValue" item="ol" separator="and"> <@jspEl 'ol.condition'/>
		       <!-- 这里的 ol.valuelist 的ol对象是map所以这边不能取 ol.values 因为map有values的方法  这是个坑 zsg -->
		       <foreach collection="ol.valuelist" open="(" close=")" item="olv" separator=","><@mapperEl 'olv'/></foreach>
		     </foreach>
		     
		     <if test="(o.criteriaWithoutValue.size!=0 or o.criteriaWithSingleValue.size!=0 or o.criteriaWithListValue.size!=0) and o.criteriaWithBetweenValue.size!=0">
		       and
		     </if>
		     <foreach collection="o.criteriaWithBetweenValue" item="ob" separator="and"> <@jspEl 'ob.condition'/> <@mapperEl 'ob.valuelist[0]'/> and <@mapperEl 'ob.valuelist[1]'/> </foreach>
		    )
		   </if>
	 	  </foreach>
 	   </trim>
	</sql>
	
	<!-- 表基础字段 -->
    <sql id="Base_Column_List">
        <#list table.columnList as column>
        ${column.fieldName}<#if column_has_next>,</#if>
        </#list>
    </sql>
    
    <!-- 根据对象插入数据 -->
    <!-- 有tableId的支持分表，分表要在对象里面传入tableId -->
    <insert id="insert" parameterType="${NamespaceDomain}.${Po}Entity">
        INSERT INTO 
        <#if tableSplit=true>
        ${tableName}_<@jspEl 'tableId'/>
        <#else>
        ${tableName}
        </#if>
        (
        	<#assign hasData=false>
		    <#list table.columnList as column>
		    <#if column.primaryKey==false>
		    	<#if hasData==false>
		    ${column.fieldName}
	  		<#assign hasData=true>
	  			<#else>
		    ,${column.fieldName}
		    	</#if>
		    </#if>
		    </#list>
        ) VALUES (
        	<#assign hasData=false>
		    <#list table.columnList as column>
		    <#if column.primaryKey=false>
		    <#if hasData==false><#assign hasData=true><#else>,</#if>${'#'}{${column.columnName?uncap_first}}
		    </#if>
		    </#list>
        )
        <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
        SELECT LAST_INSERT_ID() AS ID
        </selectKey>
    </insert>
    
    <!-- 根据对象插入不为空的字段数据 -->
    <!-- 有tableId的支持分表，分表要在对象里面传入tableId -->
    <insert id="insertNotNull" parameterType="${NamespaceDomain}.${Po}Entity">
    insert into 
    <#if tableSplit=true>
    ${tableName}_<@jspEl 'tableId'/>
    <#else>
    ${tableName}
    </#if>
    <trim prefix="(" suffix=")" suffixOverrides=",">
     <#list table.columnList as column>
	    <#if column.primaryKey=false>
	       <#if column.columnClassName=='java.lang.String'>
	          <if test="${column.columnName}!=null and ${column.columnName}!=''">
		       ${column.fieldName},
		      </if>
	       <#else>
	          <if test="${column.columnName}!=null">
		       ${column.fieldName},
		      </if>
	       </#if>
	    </#if>
	 </#list>
    </trim>
    values
    <trim prefix="(" suffix=")" suffixOverrides=",">
     <#list table.columnList as column>
        <#if column.columnClassName=='java.lang.String'>
          <if test="${column.columnName}!=null and ${column.columnName}!=''">
	       ${'#'}{${column.columnName?uncap_first}},
	      </if>
       <#else>
          <if test="${column.columnName}!=null">
	       ${'#'}{${column.columnName?uncap_first}},
	      </if>
       </#if>
	 </#list>
    </trim>
    <selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
    SELECT LAST_INSERT_ID() AS ID
    </selectKey>
  </insert>
  
  <!-- 批量插入数据 -->
  <!-- 有tableId的支持分表，分表要在传入的参数增加批量增加的tableId -->
  <#if tableSplit=true>
	<insert id="batchInsert" parameterType="java.util.Map">
	INSERT INTO ${tableName}_<@jspEl 'tableId'/>
	(
	    <#list table.columnList as column>
	    <#if column.primaryKey==false>
	   	${column.fieldName}<#if column_has_next>,</#if>
	    </#if>
	    </#list>
	) VALUES
		<foreach collection="list" item="item" index="idx" separator="," > 
			(
			<#list table.columnList as column>
			<#if column.primaryKey==false>
	        ${"#{"}item.${column.columnName}${"}"}<#if column_has_next>,</#if>
	        </#if>
	        </#list>
	        )
		</foreach> 
	</insert>
  <#else>
    <insert id="batchInsert">
	INSERT INTO ${tableName}
    (
	    <#list table.columnList as column>
	    <#if column.primaryKey==false>
	   	${column.fieldName}<#if column_has_next>,</#if>
	    </#if>
	    </#list>
    ) VALUES
		<foreach collection="list" item="item" index="idx" separator="," > 
			(
			<#list table.columnList as column>
			<#if column.primaryKey==false>
	        ${"#{"}item.${column.columnName}${"}"}<#if column_has_next>,</#if>
	        </#if>
	        </#list>
	        )
		</foreach> 
	</insert>
  </#if>
  
    <!-- 更新数据 -->
    <!-- 有tableId的支持分表，分表要在对象里面传入tableId -->
    <update id="update" parameterType="${NamespaceDomain}.${Po}Entity">
        UPDATE 
        <#if tableSplit=true>
        ${tableName}_<@jspEl 'tableId'/>
        <#else>
        ${tableName}
        </#if>
        <trim prefix="SET" suffixOverrides=",">
            <#list table.columnList as column>
		    <#if column.primaryKey=false>
		    ${column.fieldName}=${'#'}{${column.columnName?uncap_first}},
		    </#if>
		    </#list>
        </trim>
        WHERE
        <#assign hasData=false>
        <#list table.columnList as column>
		<#if column.primaryKey>
        ${column.fieldName} = ${'#'}{${column.columnName?uncap_first}}<#if hasData==false><#assign hasData=true><#else> AND </#if>
        </#if>
		</#list>
    </update>
	
	<!-- 根据对象更新不为空的字段数据 -->
	<!-- 有tableId的支持分表，分表要在对象里面传入tableId -->
    <update id="updateNotNull" parameterType="${NamespaceDomain}.${Po}Entity">
        UPDATE 
        <#if tableSplit=true>
        ${tableName}_<@jspEl 'tableId'/>
        <#else>
        ${tableName}
        </#if>
        <trim prefix="SET" suffixOverrides=",">
            <#list table.columnList as column>
              <#if column.primaryKey=false>
               <#if column.columnClassName=='java.lang.String'>
		          <if test="${column.columnName?uncap_first}!=null and ${column.columnName?uncap_first}!=''">
	                 ${column.fieldName} = <@mapperEl ''+column.columnName?uncap_first />,
	              </if>
		       <#else>
		          <if test="${column.columnName?uncap_first}!=null">
	                 ${column.fieldName} = <@mapperEl ''+column.columnName?uncap_first />,
	              </if>
		       </#if>
            </#if>
		    </#list>
        </trim>
        <#list table.columnList as column>
	    <#if column.primaryKey=true>
        WHERE n_id=<@mapperEl column.columnName?uncap_first />
        </#if>
	    </#list>
    </update>
    
    <!-- 根据condition条件更新数据 -->
    <!-- 有tableId的支持分表，分表要在UpdateParams对象里面传入tableId -->
    <update id="updateCustom" parameterType="com.weigou.dal.base.utils.UpdateParams">
      UPDATE 
      <#if tableSplit=true>
      <@jspEl 'tableName'/>_<@jspEl 'tableId'/>
      <#else>
      <@jspEl 'tableName'/>
      </#if>
      
      <trim prefix="SET" suffixOverrides=",">
    	<foreach collection="fields" item="item" index="idx" separator="," >
        <@jspEl 'item'/> <@mapperEl "values["+"$"+"{idx}]"/>
        </foreach>
      </trim>
	  <include refid="Conditions_Where_Clause" />		
    </update>
    
    <!-- 批量更新数据 list[UpdateParams] -->
    <!-- 有tableId的支持分表，分表要在每个UpdataParams对象里面传入tableId -->
    <update id="batchUpdate">
      <foreach collection="list" item="uplist" index="index" separator=";" >
        UPDATE 
        <#if tableSplit=true>
        <@jspEl 'uplist.tableName'/>_<@jspEl 'uplist.tableId'/>
        <#else>
        <@jspEl 'uplist.tableName'/>
        </#if>
        
        <trim prefix="SET" suffixOverrides=",">
    	  <foreach collection="uplist.fields" item="upfiled" index="idx" separator="," >
          <@jspEl 'upfiled'/> <@mapperEl "uplist.values["+"$"+"{idx}]"/>
          </foreach>
        </trim>
	    <include refid="Conditions_Where_Clause" />		
	  </foreach>
    </update>
    
    <!-- 按Id删除 -->
    <!-- 有tableId的支持分表，分表要在参数里面传入tableId -->
    <#if tableSplit=true>
	    <delete id="deleteById" parameterType="java.util.Map">
	        DELETE FROM ${tableName}_<@jspEl 'tableId'/>
	        <trim prefix="WHERE" prefixOverrides="AND | OR">
	            <#list table.columnList as column>
				<#if column.primaryKey>
	            AND ${column.fieldName} = <@mapperEl 'id'/>
	            </#if>
				</#list>
	        </trim>
	    </delete>
    <#else>
	    <delete id="deleteById">
	        DELETE FROM ${tableName}
	        <trim prefix="WHERE" prefixOverrides="AND | OR">
	            <#list table.columnList as column>
				<#if column.primaryKey>
	            AND ${column.fieldName} = <@mapperEl 'id'/>
	            </#if>
				</#list>
	        </trim>
	    </delete>
    </#if>

    <!--根据list(ids)删除对象-->
    <!-- 有tableId的支持分表，分表要在Conditions对象里面传入tableId -->
    <#if tableSplit=true>
	    <delete id="deleteByIds" parameterType="java.util.Map">
	        DELETE FROM ${tableName}_<@jspEl 'tableId'/>
	        WHERE id in
	        <foreach collection="ids" item="id" open="(" separator="," close=")"><@mapperEl 'id'/></foreach>
	    </delete>
    <#else>
	    <delete id="deleteByIds">
	        DELETE FROM ${tableName}
	        WHERE id in
	        <foreach collection="ids" item="id" open="(" separator="," close=")"><@mapperEl 'id'/></foreach>
	    </delete>
    </#if>
    

	<!--根据自定义删除对象-->
	<!-- 有tableId的支持分表，分表要在Conditions对象里面传入tableId -->
    <delete id="deleteCustom" parameterType="com.weigou.dal.base.utils.Conditions">
        DELETE FROM 
        <#if tableSplit=true>
        ${tableName}_<@jspEl 'tableId'/>
        <#else>
        ${tableName}
        </#if>
        <include refid="Conditions_Where_Clause" />
    </delete>
    
    <!-- 根据id获取数据 -->
    <!-- 有tableId的支持分表，分表要在参数Map对象里面传入tableId -->
    <#if tableSplit=true>
       <select id="getById" parameterType="java.util.Map" resultMap="BaseResultMap">
	        SELECT
		    <include refid="Base_Column_List" />
	        FROM ${tableName}_<@jspEl 'tableId'/>
	        WHERE 
	        n_id = <@mapperEl 'id'/>
	        LIMIT 0,1
	    </select>
    <#else>
	    <select id="getById" parameterType="${idJavaType}" resultMap="BaseResultMap">
	        SELECT
		    <include refid="Base_Column_List" />
	        FROM ${tableName}
	        WHERE 
	        n_id = <@mapperEl 'id'/>
	        LIMIT 0,1
	    </select>
    </#if>

    <!-- 根据条件获取数据 -->
    <!-- 有tableId的支持分表，分表要在参数Conditions对象里面传入tableId -->
    <select id="getBy" parameterType="com.weigou.dal.base.utils.Conditions" resultMap="BaseResultMap">
        SELECT
        <choose>
           <when test="returnFields != null">
           <foreach collection="returnFields" item="f" separator=","><@jspEl 'f'/></foreach>
           </when>
           <otherwise>
	       <include refid="Base_Column_List" />
           </otherwise>
        </choose>
        FROM 
        <#if tableSplit=true>
        <@jspEl 'tableName'/>_<@jspEl 'tableId'/>
        <#else>
        <@jspEl 'tableName'/>
        </#if>
        
        <include refid="Conditions_Where_Clause" />
        LIMIT 0,1
    </select>
    
    <!-- 适合分页或list排序等的查询 -->
    <!-- 有tableId的支持分表，分表要在参数Conditions对象里面传入tableId -->
    <select id="listByCustom"  parameterType="com.weigou.dal.base.utils.Conditions" resultMap="BaseResultMap">
        SELECT
        <if test="distinct!=false">
        DISTINCT
        </if>
        <choose>
           <when test="returnFields != null">
           <foreach collection="returnFields" item="f" separator=","><@jspEl 'f'/></foreach>
           </when>
           <otherwise>
	       <include refid="Base_Column_List" />
           </otherwise>
        </choose>
        FROM 
        <#if tableSplit=true>
        <@jspEl 'tableName'/>_<@jspEl 'tableId'/>
        <#else>
        <@jspEl 'tableName'/>
        </#if>
        
        <include refid="Conditions_Where_Clause" />
        <if test="orderByClause!=null"><@jspEl 'orderByClause'/></if> 
        <if test="limit!=null"><@jspEl 'limit'/></if>
    </select>
    
    <!-- 按照条件查询数据条数 -->
    <!-- 有tableId的支持分表，分表要在参数Conditions对象里面传入tableId -->
    <select id="listPageCount" parameterType="com.weigou.dal.base.utils.Conditions" resultType="${idJavaType}">
        SELECT count(*) FROM 
        <#if tableSplit=true>
        <@jspEl 'tableName'/>_<@jspEl 'tableId'/>
        <#else>
        <@jspEl 'tableName'/>
        </#if>
        
        <include refid="Conditions_Where_Clause" />
    </select>
    
    <!-- 获取最大的自增id -->
    <!-- 有tableId的支持分表，分表要在参数Conditions对象里面传入tableId -->
    <#if tableSplit=true>
       <select id="selectMaxId" parameterType="com.weigou.dal.base.utils.Conditions" resultType="${idJavaType}">
	        SELECT
	        MAX(id)
	        FROM ${tableName}_<@jspEl 'tableId'/>
	    </select>
    <#else>
	    <select id="selectMaxId" resultType="${idJavaType}">
	        SELECT
	        MAX(id)
	        FROM ${tableName}
	    </select>
    </#if>
    
</mapper>